#!/bin/bash

# Copyright © Huawei Technologies Co., Ltd. 2010-2020. All rights reserved.
# 1. raised up by ascend_monitor process when device boot.
# 2. upgrade aicpu packet to device.
# 3. restore network configuration.
# 4. this script is raised by user root, so can't not open other scripts here.
#    because if using other script here, they also has root authority,this takes 
#    secuity risk.
set +e

install_info="/etc/ascend_install.info"
install_info_key_array=("Driver_Install_Type" "UserName" "UserGroup" "Driver_Install_Path_Param" "Firmware_Install_Path_Param" "Firmware_Install_Type")
log_file="/var/log/device_boot_init.log"

log() {
        if [ ! -f "$log_file" ]; then
            touch $log_file
            chmod 600 $log_file
        fi
        chmod 600 $log_file
        cur_date=$(date +"%Y-%m-%d %H:%M:%S")
        echo "[aicpu] [$cur_date] "$1 >> $log_file
}

getInstallParam() {
    local _key="$1"
    local _file="$2"
    local _param

    if [ ! -f "${_file}" ];then
        exit 1
    fi

    for key_param in "${install_info_key_array[@]}"; do
        if [ ${key_param} == ${_key} ]; then
            _param=`grep -r "${_key}=" "${_file}" | cut -d"=" -f2-`
            break;
        fi
    done
    echo "${_param}"
}

network_cfg_recovery() {
        log "slot_id is ${1}, hccn_tool recovery"
        nohup "${install_path}"/driver/tools/hccn_tool -i ${1} -cfg recovery > /dev/null 2>&1 &
}

# main process start.
Driver_Install_Path_Param=$(getInstallParam "Driver_Install_Path_Param" "${install_info}")
install_path="${Driver_Install_Path_Param}"

log "<<<<<<<< device_${1} boot init >>>>>>>>"
log "device boot init start, physical id: $1, ip_addr: 127.0.0.1, user: root"

# restore network cfg
if [ -f "${install_path}"/driver/tools/hccn_tool ];then
	log "<<<<<<<< device_${1} network cfg recovery start >>>>>>>>"
	network_cfg_recovery ${1}
	log "<<<<<<<< device_${1} network cfg recovery end >>>>>>>>"
fi
